package com.lzy.cloud.ticket.admission.service.Impl;

import com.google.gson.JsonObject;
import com.lzy.cloud.ticket.admission.constant.DFContant;
import com.lzy.cloud.ticket.admission.dtos.*;
import com.lzy.cloud.ticket.admission.service.TicketService;
import com.lzy.cloud.ticket.admission.util.SendUtil;
import com.lzy.cloud.ticket.admission.util.URLConfig;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.framework.core.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 门票
 *
 * @author tengyong
 */
@Service
@Slf4j
public class TicketServiceImpl implements TicketService {


    /**
     * 门票：景点列表查询
     *
     * @param selectTicketListListDto
     */
    public Result scenicList(SelectTicketListListDto selectTicketListListDto) {
        log.info("****************************  门票：景点列表查询  start *******************************************\r\n");

        // 请求的接口地址
        String url = "Ticket/scenicList";

        // 设置请求的参数
        Map<String, Object> params = new HashMap<>();
        params.put("page", selectTicketListListDto.getPageNum());
        params.put("pageSize", selectTicketListListDto.getPageSize());
        params.put("key", selectTicketListListDto.getKey());

        String json = SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);

        List<ScenicTicketsDto> scenicTicketsDtoList = new ArrayList<>();
        if (json != null) {
            /*获取返回状态码*/
            JSONObject object = JSONObject.fromObject(json);
            String errorCode = object.getString("errorCode");
            String msg = object.getString("msg");
            String resultJson = object.getString("data");
            if (errorCode != null && resultJson != null) {
                ScenicInfoDto scenicInfoDto = DozerUtils.map(object, ScenicInfoDto.class);
                for (ScenicTicketsDto scenicTicketsDto : scenicInfoDto.getData().getRows()) {
                    scenicTicketsDtoList.add(scenicTicketsDto);
                }
            }
        }
        log.info("\r\n****************************  门票：景点列表查询  end *******************************************");
        return new Result(scenicTicketsDtoList);
    }

    /**
     * 门票:景点详情
     *
     * @param scenicDetailDto
     */
    public Result scenicDetail(ScenicDetailDto scenicDetailDto) {
        log.info("****************************  门票：景点详情  start *******************************************\r\n");

        // 请求的接口地址
        String url = "Ticket/scenicDetail";

        // 设置请求的参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("scenicId", scenicDetailDto.getScenicId());

        String json = SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);
        List<ScenicDetailDtos> scenicDetailDtosList = new ArrayList<>();
        try {
            ScenicDetailDtos scenicDetailDtos = DozerUtils.map(JSONObject.fromObject(json), ScenicDetailDtos.class);
            String noteBook = new String(scenicDetailDtos.getData().getBookNotice().getBytes("UTF-8"), "UTF-8");
            scenicDetailDtos.getData().setBookNotice(noteBook);
            scenicDetailDtosList.add(scenicDetailDtos);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.info("\r\n****************************  门票：景点详情  end *******************************************");
        }
        return new Result(scenicDetailDtosList);
    }

    /**
     * 门票：门票详情
     *
     * @param selectTicketDetailDto
     */
    public Result detail(SelectTicketDetailDto selectTicketDetailDto) {
        log.info("****************************  门票：门票详情  start *******************************************\r\n");

        // 请求的接口地址
        String url = "Ticket/detail";

        // 设置请求的参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("productId", selectTicketDetailDto.getProductId());

        String json = SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);
        List<TicketDetailDtos>  list = new ArrayList();
        TicketDetailDtos ticketDetailDtos = DozerUtils.map(JSONObject.fromObject(json),TicketDetailDtos.class);
        list.add(ticketDetailDtos);
        log.info("\r\n****************************  门票：门票详情  end *******************************************");
        return new Result(list);
    }

    /**
     * 门票：创建订单(新)
     *
     * @param request
     */
    public Result createOrderNew(String request) {
        Ensure.that(request).isNotNull("");
        TicketCreateOrderDto ticketCreateOrderDto = (TicketCreateOrderDto) JsonUtils.JSONToObj(request, TicketCreateOrderDto.class);
//        JSONObject object = JSONObject.fromObject(request);
        log.info("****************************  门票：创建订单(新)  start *******************************************\r\n");
        // 请求的接口地址
        String url = "Ticket/createOrderNew";
        // 设置请求的参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sourceOrderId", ticketCreateOrderDto.getSourceOrderId());
        params.put("acctId", DFContant.ACCTID); //
        params.put("productId", ticketCreateOrderDto.getProductId()); //
        params.put("startTime", ticketCreateOrderDto.getStartTime()); //
        params.put("bookNumber", ticketCreateOrderDto.getBookNumber()); //
        Map<String, Object> contact = new HashMap<String, Object>();
        contact.put("contactName", ticketCreateOrderDto.getContact().getContactName());
        contact.put("contactTel", ticketCreateOrderDto.getContact().getContactTel());
        params.put("contact", contact); //

        String json = SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);
        System.out.println(json);
        BaseDFResult baseDFResult = DozerUtils.map(JSONObject.fromObject(json),BaseDFResult.class);
        log.info("\r\n****************************  门票：创建订单(新)  end *******************************************");
        return new Result(baseDFResult);
    }

    /**
     * 门票：取消订单
     *
     * @param request
     */
    public Result cancelOrder(String request) {
        Ensure.that(request).isNotNull("");
        CancelOrderDto cancelOrderDto = (CancelOrderDto) JsonUtils.JSONToObj(request, CancelOrderDto.class);

        log.info("****************************  门票：取消订单  start *******************************************\r\n");

        // 请求的接口地址
        String url = "Ticket/cancelOrder";

        // 设置请求的参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderId", cancelOrderDto.getOrderId()); //
        params.put("remark", cancelOrderDto.getRemark()); //

        SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);

        log.info("\r\n****************************  门票：取消订单  end *******************************************");
        return new Result();
    }

    /**
     * 门票：出票（代扣）
     *
     * @param submitTicketDto
     */
    public void submitOrder(SubmitTicketDto submitTicketDto) {
        log.info("****************************  门票：出票（代扣）  start *******************************************\r\n");

        // 请求的接口地址
        String url = "Ticket/submitOrder";

        // 设置请求的参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("acctId", DFContant.ACCTID); //
        params.put("orderId", submitTicketDto.getOrderId()); //
        params.put("payType", submitTicketDto.getPayType()); //
        params.put("pay", submitTicketDto.getPay()); //
        params.put("platform", submitTicketDto.getPlatform()); //

        String json = SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);


        log.info("\r\n****************************  门票：出票（代扣）  end *******************************************");
    }

    /**
     * 门票：订单详情
     *
     * @param request
     */
    public Result orderDetail(String request) {
        log.info("****************************  门票：订单详情  start *******************************************\r\n");
        OrderDto orderDto = (OrderDto) JsonUtils.JSONToObj(request, OrderDto.class);
        // 请求的接口地址
        String url = "Ticket/orderDetail";

        // 设置请求的参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("acctId", "100012"); //
        params.put("orderId", orderDto.getOrderId()); //

        String json = SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);
        OrderDetailDto orderDetailDto = DozerUtils.map(JSONObject.fromObject(json),OrderDetailDto.class);
        log.info("\r\n****************************  门票：订单详情  end *******************************************");
        return new Result(orderDetailDto);
    }

    /**
     * 门票：订单状态
     *
     * @param request
     */
    public Result orderStatus(String request) {
        Ensure.that(request).isNotNull("");
        OrderDto orderDto = (OrderDto) JsonUtils.JSONToObj(request, OrderDto.class);
        log.info("****************************  门票：订单状态  start *******************************************\r\n");

        // 请求的接口地址
        String url = "Ticket/orderStatus";

        // 设置请求的参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("acctId", DFContant.ACCTID); //
        params.put("orderId", orderDto.getOrderId()); //

        String json = SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);
        OrderStatus orderStatus = DozerUtils.map(JSONObject.fromObject(json),OrderStatus.class);

        log.info("\r\n****************************  门票：订单状态  end *******************************************");
        return new Result(orderStatus);
    }

    /**
     * 门票：退票申请
     *
     * @param apiKey
     * @param secretKey
     */
    public void refundOrder(String apiKey, String secretKey) {
        log.info("****************************  门票：退票申请  start *******************************************\r\n");

        // 请求的接口地址
        String url = "Ticket/refundOrder";

        // 设置请求的参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("remark", "100012"); //
        params.put("orderId", 33294814); //

        SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);

        log.info("\r\n****************************  门票：退票申请  end *******************************************");
    }

    /**
     * 门票：门票核销查询
     *
     * @param request
     */
    public Result verifyOrder(String request) {
        Ensure.that(request).isNotNull("");
        OrderDto orderDto = (OrderDto) JsonUtils.JSONToObj(request, OrderDto.class);
        log.info("****************************  门票：门票核销查询  start *******************************************\r\n");

        // 请求的接口地址
        String url = "Ticket/verifyOrder";

        // 设置请求的参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderId", orderDto.getOrderId()); //

        String json = SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);
        VerifyOrderDto verifyOrderDto = DozerUtils.map(JSONObject.fromObject(json),VerifyOrderDto.class);
        log.info("\r\n****************************  门票：门票核销查询  end *******************************************");
        return new Result(verifyOrderDto);
    }

    /**
     * 门票：特权门票查询
     *
     * @param apiKey
     * @param secretKey
     */
    public void privilegeTicketList(String apiKey, String secretKey) {
        log.info("****************************  门票：特权门票查询  start *******************************************\r\n");

        // 请求的接口地址
        String url = "Ticket/privilegeTicketList";

        // 设置请求的参数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", 1); //
        params.put("pageSize", 1); //

        SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);

        log.info("\r\n****************************  门票：特权门票查询  end *******************************************");
    }

    /**
     * 查询门票是否可退
     *
     * @param request
     */
    public Result canRefundTicket(String request) {
        Ensure.that(request).isNotNull("");
        OrderDto orderDto = (OrderDto) JsonUtils.JSONToObj(request, OrderDto.class);
        log.info("****************************  门票：是否可退查询  start *******************************************\r\n");

        //请求的接口地址
        String url = "Ticket/canRefund";

        //设置请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("orderId", orderDto.getOrderId());
        String json = SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);
        CanRefundDto canRefundDto = DozerUtils.map(JSONObject.fromObject(json),CanRefundDto.class);
        log.info("\r\n****************************  门票：是否可退查询  end *******************************************");
        return new Result(canRefundDto);
    }

    /**
     * 在线退票申请
     */
    public Result onLineRefund(String request) {
        Ensure.that(request).isNotNull("");
        log.info("****************************  门票：在线申请退票  start *******************************************\r\n");
        RefundTicketDto refundTicketDto = (RefundTicketDto) JsonUtils.JSONToObj(request, RefundTicketDto.class);
        //请求接口地址
        String url = "Ticket/tuiPiao";

        //设置请求参数
        Map<String, Object> params = new HashMap<>();
        params.put("orderId", refundTicketDto.getOrderId());
        params.put("causeType", refundTicketDto.getCauseType());
        params.put("causeContent", refundTicketDto.getCauseContent());
        String json = SendUtil.sendPostConn(URLConfig.URL + url, params, DFContant.API_KEY, DFContant.SECRECT_KEY);
        RefundReturnDto refundReturnDto = DozerUtils.map(JSONObject.fromObject(json),RefundReturnDto.class);
        log.info("\r\n****************************  门票：在线申请退票  end *******************************************");
        return new Result(refundReturnDto);
    }


}
